// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Create group in marker
// -----------------------------------------------
if (!isServer) exitWith {};

private [
	"_indexparameters",
	"_position",
	"_marker",
	"_arrayofvehicle",
	"_typeofgroup",
	"_vehicle",
	"_group",
	"_sizeofgroup",
	"_alert",
	"_localalert",
	"_instances",
	"_list",
	"_parameters",
	"_nbparameters",
	"_motorized"
];

_parameters = [
	"_marker",
	"_typeofgroup",
	"_motorized"
];

_indexparameters = 0;
_nbparameters = count _this;
{
	if (_indexparameters <= _nbparameters) then {
		call compile format["%1 = _this select %2;", _x, _indexparameters];
	};
	_indexparameters = _indexparameters + 1;
} foreach _parameters;

_instances = [];

if (_motorized) then {
	_position = [_marker, "onground", "onflat"] call WC_fnc_createpositioninmarker;
	_arrayofvehicle = [_position, 0, _typeofgroup, east] call BIS_fnc_spawnVehicle;
	_vehicle = _arrayofvehicle select 0;
	_group   = _arrayofvehicle select 2;

	diag_log format ["WARCONTEXT: CREATING VEHICLE %2 IN ZONE %1", _marker, _typeofgroup];

	//A3 TODO: setVehicleInit
	_vehicle setVehicleInit "this lock true; [this] spawn EXT_fnc_atot;";
	processInitCommands;
	_vehicle setDir (random 360);
	_vehicle setFuel wcenemyglobalfuel;

	wcgarbage = [_vehicle] spawn WC_fnc_vehiclehandler;
} else {
	_position = [_marker, "onground"] call WC_fnc_createpositioninmarker;
	_group = [_typeofgroup, east, _position] call WC_fnc_popgroup;

	diag_log format ["WARCONTEXT: CREATING A GROUP %2 IN ZONE %1 OF SIZE %3", _marker, _typeofgroup, _sizeofgroup];
};

wcgarbage = [_group] spawn WC_fnc_grouphandler;

_group allowFleeing 0;

if (_motorized) then {
	// By default, all vehicles are in depot mode :)
	if (random 1 > 0.05) then {
		_localalert = (20 + random 70);
		{
			//if (random 1 > 0.2) then {
				[_x] orderGetIn false;
			//	_instances = _instances + [[_x] spawn WC_fnc_dosillything];
			//};
			sleep 0.1;
		} foreach (units _group);
		_instances = [_group, (position (leader _group)), 30] spawn WC_fnc_patrol;

		// Use while instead waitUntil - performance leak
		// Soldier dont go into vehicle while no alert
		_alert = false;
		while {!_alert} do {
			if (wcalert > _localalert) then {
				_alert = true;
			};
			if (behaviour (leader _group) == "COMBAT") then {
				_alert = true;
			} else {
				sleep (1 + random 5);
			};
		};

		terminate _instances;
		//{
		//	terminate _x;
		//} foreach _instances;
		(units _group) orderGetIn true;

		// Use while instead waitUntil - performance leak
		while {(count (crew _vehicle)) != (count (units _group))} do {
			sleep 1;
		};
	};

	wcgarbage = [_vehicle, _marker, "showmarker"] execVM "extern\ups.sqf";

	if ((typeOf _vehicle) in wcsabotagelist) then {
		_vehicle setDamage 1;
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONCOMMANDEMENT", format ["%1 sabotaged exploded", (typeOf _vehicle)]];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
	};
} else {
	if (random 1 > 0.4) then {
		_list = nearestObjects [_position, ["House"], 70];
		if (count _list > 10) then {
			wcgarbage = [(leader _group), _marker, "showmarker", "fortify"] spawn EXT_fnc_upsmon;
			//wcgarbage = [_group, (position (leader _group)), wcdistance] spawn WC_fnc_patrol;
		} else {
			wcgarbage = [(leader _group), _marker, "showmarker"] spawn EXT_fnc_upsmon;
			//wcgarbage = [_group, (position (leader _group)), wcdistance] spawn WC_fnc_patrol;
		};
	} else {
		wcgarbage = [(leader _group), _marker, "showmarker"] spawn EXT_fnc_upsmon;
		//wcgarbage = [_group, (position (leader _group)), wcdistance] spawn WC_fnc_patrol;
	};
};